#include <stdio.h>/*该文件包含pringtf()等函数*/
#include <stdlib.h>/*该文件包含exit()等函数*/
#include <malloc.h>/*该文件包含malloc()等函数*/

typedef int DataType;/*定义DataType为int*/


typedef struct Node
{
    DataType data;
    struct Node *next;
} SLNode;
// 要求将所有代码写在本文件内

int ListInitiate(SLNode **head)/*任诗进2019240138*/ {
    *head = (SLNode *) malloc(sizeof(SLNode));
    if (*head == NULL)
        return 0;
    (*head)->next = NULL;
    return 1;
}

int ListLength(SLNode *head) /*石中波2019240139*/{
    int len = 0;
    if (head == NULL)
        return 0;
    if (head -> next == NULL)
        return 1;
    while (head -> next != NULL){
        head = head -> next;
        len++;
    }
    return len;
}

int ListInsert(SLNode *head, int i, DataType x) /*王醴峣2019240141*/{
    i = i+1;
    if (i < 1)
        return 0;
    SLNode *p;
    int j = 0;
    p = head;
    while (p != NULL && j < i-1) {
        p = p->next;
        j++;
    }
    if (p == NULL)
        return 0;
    SLNode *s = (SLNode *) malloc(sizeof(SLNode));
    s->data = x;
    s->next = head->next;
    head->next = s;
    return 1;
}

int ListDelete(SLNode *head, int i, DataType *x) /*王醴峣2019240141*/{
    if (i < 1)
        return 0;
    SLNode *p;
    int j=0;
    p = head;
    while (p != NULL && j < i-1){
        p = p -> next;
        j++;
    }
    if (p == NULL)
        return 0;
    if (p -> next == NULL)
        return 0;
    SLNode *q = p -> next;
    *x = q -> data;
    p -> next = q -> next;
    free(q);
    return 1;
}

int ListGet(SLNode *head, int i, DataType *x)/*王英炜2019240142*/ {
    i = i+1;
    if (i < 0)
        return 0;
    int j = 0;
    SLNode *p;
    p = head;
    while (p != NULL && j < i){
        p = p -> next;
        j++;
    }
    *x = p -> data;
    return 1;
}

int Destroy(SLNode **head)/*高天泽2019240127*/ {
    if (*head == NULL)
        return 0;
    SLNode *p = *head;
    while ((*head) -> next != NULL){
        *head = (*head) -> next;
        free(p);
        p = *head;
    }
    return 1;
}

void main(void) {
    SLNode *head;
    int i , x;
    ListInitiate(&head);/*初始化*/
    for(i = 0; i < 10; i++) {
        if(ListInsert(head, i, i+1) == 0) /*插入10个数据元素*/
        {
            printf("错误! \n");
            return;
        }
    }

    if(ListDelete(head, 4, &x) == 0) { /*删除数据元素5*/
        printf("错误! \n");
        return;
    }
    printf("%d\n", ListLength(head));

    for(i = 0; i < ListLength(head); i++) {
        if(ListGet(head, i, &x) == 0) /*取元素*/
        {
            printf("错误! \n");
            return;
        }
        else printf("%d    ", x);/*显示数据元素*/
    }

    Destroy(&head);
}
